package com.controller.back;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sound.sampled.Line;
import java.io.IOException;

@WebServlet("/VCode")
public class VCodeController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        //创建验证码
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100,4,4);
        //向session中保存验证码的真实内容
        String code = captcha.getCode();
        HttpSession session = req.getSession();
        session.setAttribute("realCode",code);

        //通过流将验证码响应回浏览器
        ServletOutputStream out = res.getOutputStream();
        captcha.write(out);
    }
}
